<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="userInfo" labelWidth="80px">
				<uni-forms-item label="紧急联系人" name="sms_name" labelWidth="90px">
					<uni-easyinput v-model="userInfo.sms_name" placeholder="请输入紧急联系人" />
				</uni-forms-item>
				<uni-forms-item label="紧急联系人电话" name="sms_mobile" labelWidth="120px">
					<uni-easyinput maxlength="11" v-model="userInfo.sms_mobile" placeholder="请输入紧急联系人电话" />
				</uni-forms-item>
				<uni-forms-item label="邮箱" name="email">
					<uni-easyinput v-model="userInfo.email" placeholder="请输入邮箱" />
				</uni-forms-item>
				<uni-forms-item label="省市区">
					<picker mode="region" @change="bindPickerAddressChange" style="width: 100%;">
						<input class="input-address" type="text" v-model="address" disabled placeholder="请选择省市区">
					</picker>
				</uni-forms-item>
				<uni-forms-item label="详细地址" name="area">
					<uni-easyinput v-model="userInfo.area" placeholder="请输入详细地址" />
				</uni-forms-item>
				<uni-forms-item label="个人介绍" name="introduction">
					<uni-easyinput type="textarea" autoHeight v-model="userInfo.introduction" placeholder="请输入个人介绍"
						:maxlength="300"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit" style="background-color: #3c9cff;">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getUserDetailInfoApi,
		editUserDetailInfoApi
	} from '@/api/index.js'
	export default {
		data() {
			return {
				userInfo: {
					sms_name: "",
					sms_mobile: "",
					email: "",
					province: "",
					city: "",
					area: "",
					introduction: ""
				},
				address: "",
				rules: {
					sms_name: {
						rules: [{
							required: true,
							errorMessage: '紧急联系人不能为空'
						}]
					},
					sms_mobile: {
						rules: [{
							required: true,
							errorMessage: '紧急联系人电话不能为空'
						}, {
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							errorMessage: '请输入正确的紧急联系人电话'
						}]
					},
					email: {
						rules: [{
							required: true,
							errorMessage: '邮箱地址不能为空'
						}, {
							format: 'email',
							errorMessage: '请输入正确的邮箱地址'
						}]
					}
				}
			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo')
			this.getUserInfo(this.userInfo.id)
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			// 获取用户信息
			async getUserInfo(id) {
				const {
					data: res
				} = await getUserDetailInfoApi(id);
				this.userInfo = res;
				if (res.province) {
					this.address = res.province + res.city;
				}
				console.log(this.userInfo);
			},
			// 选择地区
			bindPickerAddressChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				const {
					value
				} = e.detail
				this.address = value[0] + value[1] + value[2];
				this.userInfo.province = value[0];
				this.userInfo.city = value[1];
			},


			submit(ref) {
				this.$refs.form.validate().then(res => {
					// console.log(this.userInfo);
					const {
						id,
						sms_name,
						sms_mobile,
						email,
						province,
						city,
						area,
						introduction
					} = this.userInfo;
					let params = {
						id,
						sms_name,
						sms_mobile,
						email,
						province,
						city,
						area,
						introduction
					}
					console.log('params', params);
					editUserDetailInfoApi(params).then(response => {
						console.log(response);
						// this.$modal.msgSuccess("修改成功")
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}

	.uni-forms-item__content {
		display: flex;
		align-items: center;
	}

	.uni-easyinput[disabled] {
		opacity: 1 !important;
		cursor: text !important;
		/* 其它样式属性... */
	}

	.input-address {
		box-sizing: border-box;
		flex-direction: row;
		align-items: center;
		border: 1px solid #F0F0F0;
		border-radius: 4px;
		border-color: #e5e5e5;
		background-color: #fff;
		padding-left: 10px;
		font-size: 12px;
		height: 35px;
	}
</style>